package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kang.dto.LabelLineDto;
import com.kang.dto.MessageDto;
import com.kang.dto.SeriesDto;
import com.kang.entity.Essay;
import com.kang.entity.Label;
import com.kang.entity.Park;
import com.kang.entity.SysUser;
import com.kang.mapper.EssayMapper;
import com.kang.mapper.LabelMapper;
import com.kang.mapper.ParkMapper;
import com.kang.service.LargeScreenService;
import com.kang.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 康康
 * @Date: 2023/03/26/22:38
 */
@Service
public class LargeScreenServiceImpl implements LargeScreenService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EssayMapper essayMapper;

    @Autowired
    private ParkMapper parkMapper;

    @Autowired
    private LabelMapper labelMapper;



    @Override
    public SysUser getUser() {
        SysUser sysUser = new SysUser();
        sysUser.setUsername(ThreadLocalUtil.getCurrentUser().getUsername());
        sysUser.setHeadUrl(ThreadLocalUtil.getCurrentUser().getHeadUrl());
        sysUser.setCreateTime(ThreadLocalUtil.getCurrentUser().getCreateTime());
        return sysUser;
    }

    @Override
    public List<MessageDto> getMessage() {
        List<MessageDto> resultList = new ArrayList<>();
        //今日活跃用户
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Long toDayUser = stringRedisTemplate.opsForValue().size("userCount:" + format);
        assert toDayUser != null;
        MessageDto messageDto = new MessageDto("当日用户活跃量", toDayUser.intValue(), "success", "#2ec7c9");
        resultList.add(messageDto);
        //今日文章发布量
        String plusDay = localDateTime.plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        QueryWrapper<Essay> wrapper = new QueryWrapper<>();
        wrapper.between("create_time",format,plusDay);
        Integer integer = essayMapper.selectCount(wrapper);
        MessageDto messageDto1 = new MessageDto("今日文章发布量", integer, "star-on", "#ffb980");
        resultList.add(messageDto1);
        //今日社区申请量
        QueryWrapper<Park> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time",format,plusDay);
        Integer integer2 = parkMapper.selectCount(queryWrapper);
        MessageDto messageDto4 = new MessageDto("今日社区申请量", integer2, "s-goods", "#5ab1ef");
        resultList.add(messageDto4);
        //本月活跃用户
        List<String> time = Arrays.stream(format.split("-")).collect(Collectors.toList());
        int day = Integer.parseInt(time.get(2));
        int count = 0;
        for (int i = 1; i <= day; i++) {
            Long size = stringRedisTemplate.opsForValue().size("userCount:" + time.get(0) + "-" + time.get(1) + "-" + i);
            assert size != null;
            count = count + size.intValue();
        }
        MessageDto messageDto2 = new MessageDto("本月用户活跃量", count, "success", "#2ec7c9");
        resultList.add(messageDto2);
        //总发布量
        Integer integer1 = essayMapper.selectCount(null);
        MessageDto messageDto3 = new MessageDto("文章总发布量", integer1, "star-on", "#ffb980");
        resultList.add(messageDto3);
        //总社区量
        Integer integer3 = parkMapper.selectCount(null);
        MessageDto messageDto5 = new MessageDto("总社区申请量", integer3, "s-goods", "#5ab1ef");
        resultList.add(messageDto5);
        return resultList;
    }

    @Override
    public List<SeriesDto> getSeriesDate() {
        List<SeriesDto> resultList = new ArrayList<>();
        List<Integer> list01 = new ArrayList<>();
        LocalDateTime localDateTime = LocalDateTime.now();
        //获取当前是周几
        int value = localDateTime.getDayOfWeek().getValue();
        //获取本周数据
        for (int i = value - 1; i >= 0; i--) {
            String format = localDateTime.plusDays(-i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            Long size = stringRedisTemplate.opsForValue().size("userCount:" + format);
            list01.add(size == null ? 0 :size.intValue());
        }
        SeriesDto seriesDto = new SeriesDto("本周访问量", "bar", list01);
        resultList.add(seriesDto);
        //获取上周数据
        List<Integer> list02 = new ArrayList<>();
        for (int i = 6; i >= 0; i-- ) {
            String format = localDateTime.plusDays(-value - i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            Long size = stringRedisTemplate.opsForValue().size("userCount:" + format);
            list02.add(size == null ? 0 :size.intValue());
        }
        SeriesDto seriesDto1 = new SeriesDto("上周访问量", "bar", list02);
        resultList.add(seriesDto1);
        return resultList;
    }

    @Override
    public List<LabelLineDto> getLabelLine(String value) throws ParseException {
        List<String> stringList = Arrays.stream(value.split("T")).collect(Collectors.toList());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        Date date=sdf.parse(stringList.get(0));
        Calendar rightNow=Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_YEAR,1);
        String endTime=sdf.format(rightNow.getTime());

        //文章标签集合
        List<Label> list = labelMapper.selectList(new QueryWrapper<Label>().eq("parent_id", 7));
        List<LabelLineDto> result = new ArrayList<>();
        //查询当日文章
        QueryWrapper<Essay> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time",stringList.get(0),endTime);
        List<Essay> essayList = essayMapper.selectList(queryWrapper);
        list.forEach(v -> {
            int count = 0;
            LabelLineDto labelLineDto = new LabelLineDto();
            labelLineDto.setName(v.getLabelName());
            for (Essay essay : essayList) {
                List<String> collect = Arrays.stream(essay.getLabelId().split(",")).collect(Collectors.toList());
                if (collect.contains(String.valueOf(v.getId()))) {
                    count = count + 1;
                }
            }
            labelLineDto.setValue(count);
            result.add(labelLineDto);
        });

        return result;
    }


}
